package com.linxz.ui.widgets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.linxz.ui.R;

/**
 * @author Linxz
 * time:2018年05月28日 8:39.
 * 自定义加载中、暂无数据、加载失败页面,页面异常状态下支撑刷新
 */

public class SmartLayout extends LinearLayout {

    private SwipeRefreshLayout mContentView;

    private ViewGroup mLoadingView;
    private ViewGroup mEmptyView;
    private ViewGroup mErrorView;

    private int mLoadingId;
    private int mEmptyId;
    private int mErrorId;

    private LayoutInflater mLayoutInflater;

    public SmartLayout(Context context) {
        super(context);
        init();
    }

    public SmartLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initAttrs(attrs);
        init();
    }


    protected void initAttrs(AttributeSet attrs) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.SmartLayout);
        try {
            mLoadingId = a.getResourceId(R.styleable.SmartLayout_layout_loading, R.layout.layout_loading);
            mEmptyId = a.getResourceId(R.styleable.SmartLayout_layout_empty, R.layout.ui_layout_empty);
            mErrorId = a.getResourceId(R.styleable.SmartLayout_layout_error, R.layout.ui_layout_error);
        } finally {
            a.recycle();
        }
    }

    @SuppressLint("InflateParams")
    private void init(){
        mLayoutInflater=LayoutInflater.from(getContext());
        //生成主View
        if (mLayoutInflater==null){
            return;
        }
        mContentView= (SwipeRefreshLayout) mLayoutInflater.inflate(R.layout.ui_layout_smart,null,false);
        initRefreshLayout();
        mLoadingView=mContentView.findViewById(R.id.loading);
        mEmptyView=mContentView.findViewById(R.id.empty);
        mErrorView=mContentView.findViewById(R.id.error);
        if (mLoadingId!=0){
            mLayoutInflater.inflate(mLoadingId,mLoadingView);
        }
        if (mEmptyId!=0){
            mLayoutInflater.inflate(mEmptyId,mEmptyView);
        }
        if (mErrorId!=0){
            mLayoutInflater.inflate(mErrorId,mErrorView);
        }
        mContentView.setVisibility(View.GONE);
        this.addView(mContentView);
    }

    private  void initRefreshLayout() {
        mContentView.setEnabled(false);
        mContentView.setOnRefreshListener(new SwipeRefreshLayout
            .OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (mOnRefreshListener!=null){
                    mOnRefreshListener.onRefresh();
                }
            }
        });
        mContentView.setColorSchemeResources(android.R.color.holo_orange_light, android.R.color
            .holo_blue_bright, android.R.color.holo_red_light);
        mContentView.setProgressViewOffset(true, 80, 300);
    }

    public void setLoadingView(View view){
        mLoadingView.removeAllViews();
        mLoadingView.addView(view);
    }

    public void setLoadingView(int layoutResId){
        mLoadingView.removeAllViews();
        mLayoutInflater.inflate(layoutResId,mLoadingView);
    }

    public void setEmptyView(View view){
        mEmptyView.removeAllViews();
        mEmptyView.addView(view);
    }

    public void setEmptyView(int layoutResId){
        mEmptyView.removeAllViews();
        mLayoutInflater.inflate(layoutResId,mEmptyView);
    }

    public void setErrorView(View view){
        mErrorView.removeAllViews();
        mErrorView.addView(view);
    }

    public void setErrorView(int layoutResId){
        mErrorView.removeAllViews();
        mLayoutInflater.inflate(layoutResId,mErrorView);
    }


    private void hideAll(){
        mLoadingView.setVisibility(View.GONE);
        mErrorView.setVisibility(View.GONE);
        mEmptyView.setVisibility(View.GONE);
    }

    public void showLoadingView(){
        if (mLoadingView.getChildCount()>0){
            hideAll();
            mLoadingView.setVisibility(View.VISIBLE);
            mContentView.setVisibility(View.VISIBLE);
        }

    }

    public void showEmptyView(){
        if (mEmptyView.getChildCount()>0){
            hideAll();
            mEmptyView.setVisibility(View.VISIBLE);
            mContentView.setVisibility(View.VISIBLE);
        }
    }

    public void showErrorView(){
        if (mErrorView.getChildCount()>0){
            hideAll();
            mErrorView.setVisibility(View.VISIBLE);
            mContentView.setVisibility(View.VISIBLE);
        }
    }

    public void showNormal(){
        mContentView.setVisibility(View.GONE);
    }

    public SwipeRefreshLayout getSwipRefreshLayout(){
        return mContentView;
    }


    public interface OnRefreshListener{
        void onRefresh();
    }

    private OnRefreshListener mOnRefreshListener;

    public void setOnRefreshListener(OnRefreshListener onRefreshListener){
        mContentView.setEnabled(true);
        this.mOnRefreshListener=onRefreshListener;
    }

    public void setRefreshing(boolean refresh){
        this.mContentView.setRefreshing(refresh);
    }

    public void setRefreshEnabled(boolean enabled){
        mContentView.setEnabled(enabled);
    }


}
